package com.itheima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.util.RequestContextUtil;
import com.itheima.common.vo.PageResultVo;
import com.itheima.wemedia.dto.WmMaterialPageRequestDto;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.mapper.WmMaterialMapper;
import com.itheima.wemedia.service.WmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @description <p>自媒体图文素材信息 业务实现</p>
 *
 * @version 1.0
 * @package com.itheima.wemedia.service.impl
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Value("${fastdfs-server}")
    private String fastdfsServer;

    /**
     * 素材列表 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResultVo findPage(WmMaterialPageRequestDto dto) {
        //1. 设置分页参数
        IPage<WmMaterial> pageInfo = new Page<>(dto.getPage(), dto.getSize());
        //2. 构建查询条件
        LambdaQueryWrapper<WmMaterial> lqw = new LambdaQueryWrapper<>();
        lqw.eq(dto.getIsCollection()!=0, WmMaterial::getIsCollection, dto.getIsCollection());
        // 只能查询自己的素材
        lqw.eq(WmMaterial::getUserId, RequestContextUtil.getUserId());
        //3. 分页查询
        page(pageInfo, lqw);
        //4. 补全图片的全路径
        List<WmMaterial> list = pageInfo.getRecords();
        if(!CollectionUtils.isEmpty(list)){
            // 有数据，才需要补全路径
            list.forEach(m->m.setUrl(fastdfsServer + m.getUrl()));
        }
        //5. 构建结果返回
        return PageResultVo.pageResult(dto.getPage(),dto.getSize(),pageInfo.getTotal(), pageInfo.getRecords());
    }
}
